<?

// what php version are we using?
{ $version = explode( '.', phpversion() );
	define( 'IS_PHP4', ($version[1]<5) ); }

/**
 *  this function handles loading of smutty classes as and
 *  when they're needed, but is only supported by php5 >
 *
 *  @param [className] the name of the class to load
 *
 */

function __autoload( $className ) {

	// first try the library
	$file = preg_replace( '/_/', '/', $className );
	$path = "../library/$file.class.php";
	if ( file_exists($path) ) {
		include $path;
		return true;
	}

	// then maybe a model?
	$path = "../application/models/$className.php";
	if ( file_exists($path) ) {
		// this data gets added to the class dynamically.
		// need this cause of php's static lmitation thing...
		// this is a ***VERY*** dirty hack, but it works and i
		// like the syntax it allows so it's stayin for now!
		$replace = "

function getFields() {
	return parent::getFields( '$className' );
}

function find( \$id ) {
	return parent::find( '$className', \$id );
}

function fetchAll( \$order = false, \$params = array(), \$limit = false ) {
	return parent::fetchAll( '$className', \$order, \$params, \$limit );
}

";
		// read in model class data to alter then eval
		$data = file_get_contents( $path );
		$data = preg_replace( '/<\?/', '', $data );
		$data = preg_replace( '/\?>/', '', $data );
		$data = preg_replace( '/extends Smutty_Model {/', "extends Smutty_Model { $replace ", $data );
		eval( $data );
		if ( IS_PHP4 )
			overload( $className );
		return true;
	}

	die( 'Required class file not found:' . $className );

}

class Smutty_Main {

	/**
	 *  use this to load one or more classes on demand.  php5 can
	 *  use the __autoload() function itself to have classes loaded,
	 *  but previous versions will need to load classes before
	 *  they are accessed.
	 *
	 *  @param [classes] array/string of class names
	 *
	 */

	function loadClass( $classes ) {

		static $loaded;
		if ( $loaded == null )
			$loaded = array();

		if ( !is_array($classes) )
			$classes = array( $classes );

		foreach ( $classes as $class ) {
			if ( !$loaded[$class] ) {
				$loaded[ $class ] = true;
				__autoload( $class );
			}
		}

	}

	/**
	 *  this is the entry point of the application, call this
	 *  to get everything going!!!
	 *
	 */

	function run() {

		// if we're < php5 then we need to load some classes
		// by default as the __autoload() method won't exist.
		$version = explode( '.', phpversion() );
		if ( $version[0] < 5 ) {
			// first load the Smutty classes we're gonna need
			Smutty_Main::loadClass(array(
				'Smutty_Model', 'Smutty_Database',
				'Smutty_Template', 'Smutty_Utils',
				'Smutty_Router', 'Smutty_Data',
				'Smutty_Controller', 'Smutty_Config',
				'Smutty_Exception', 'Smutty_SmuttyTemplate'
			));
			// then load the users models so they're available
			$d = opendir( '../application/models/' );
			while ( $f = readdir($d) )
				if ( preg_match('/(.*)\.php$/',$f,$matches) )
					Smutty_Main::loadClass( $matches[1] );
			closedir( $d );
		}

		// grab the data we'll need
		Smutty_Data::init();

		// then create the router and dispatch the request
		$router = Smutty_Router::getInstance();
		$router->dispatch();

	}

}

?>